package cn.jiyun.controller;
import cn.jiyun.common.config.Result;
import cn.jiyun.entity.XmUserLogin;
import cn.jiyun.service.IXmUserLoginService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-20
 */
@RestController
@RequestMapping("/xm-user-login/xm-login")
public class XmUserLoginController {

    @Autowired
    private IXmUserLoginService xmUserLoginService;

    @RequestMapping("/login")
    public Result Login(@RequestBody XmUserLogin user){
        LambdaQueryWrapper<XmUserLogin> qr = new LambdaQueryWrapper<>();
        qr.eq(XmUserLogin::getUsername,user.getUsername());
        XmUserLogin one = xmUserLoginService.getOne(qr);
        if (one==null){
            return Result.fail("账号或手机号不存在！");
        }
        if (!one.getPassword().equals(user.getPassword())){
            return Result.fail("密码错误！");
        }
        return Result.ok(one);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        XmUserLogin one = xmUserLoginService.getById(id);
        return Result.ok(one);
    }

    @RequestMapping("/updatePassword")
    public Result updatePassword(String password1,Integer id){
        XmUserLogin xmUserLogin = xmUserLoginService.getById(id);
        xmUserLogin.setPassword(password1);
        xmUserLoginService.updateById(xmUserLogin);
        return Result.ok();
    }

    @RequestMapping("/findPage")
    public Result findPage(Integer pageNum,Integer pageSize,@RequestBody XmUserLogin user){
        Page page = xmUserLoginService.findPage(pageNum,pageSize,user);
        return Result.ok(page);
    }


}
